<?php
/**
 * 
 * This class controls the scraping logistics, it reads the querystring in URL and takes appropriate action depending on value in querystring.
 * 
 * @author Jarmo Baltzar
 * @author Vicktor Karlsson
 * 
 * @version 1.0
 */
	class FzXmlController
	{
		/**
		 * This method initiates necessary classes, and checks what option user has chosen in the webpage. Depending on the choices this class
		 * sends the appropriate URL to scraping class setUrl method.
		 * 
		 * Example of how to initiate FzXmlController and how to call DoControll Method.
		 * <code>
		 * 	<?php
		 * 	$fzCntrl = new FzXmlController();
		 *	$fzXhtml = $fzCntrl->DoControll();
		 * 	?>
		 * </code>
		 * 
		 * @name DoControll()
		 * @param none
		 * @return the RSS feed XML
		 */
		public function DoControll()
		{
			$xhtml = "";
			$title = "Workshop 1";
			$errorMessage = "";
		
			$scraping = new Scraping();
			
			// Type of news to read in, depends on querystring 
			if(isset($_GET['type']) && $_GET['type'] == "all")
			{
				$scraping->setUrl("http://www.fz.se/core/rss/fznews_rss20.xml");
			}
			elseif(isset($_GET['type']) && $_GET['type'] == "console")
			{
				$scraping->setUrl("http://www.fz.se/core/rss/fznews_console_rss20.xml");
			}
			elseif(isset($_GET['type']) && $_GET['type'] == "pc")
			{
				$scraping->setUrl("http://www.fz.se/core/rss/fznews_pcmac_rss20.xml");
			}
			else
			{
				$xhtml .= null;
			}
			
			// Retrive result with curl
			$result = $scraping->retriveXml();
									
			// Check if result is atom or rss
			$fzXmlView = new FzXmlView();
			
			if($result != null)
			{		
				if($result->channel)
				{
					$xhtml .= $fzXmlView->GenerateFromRss($result);
				}
				elseif($result->entry)
				{
					$xhtml .= $fzXmlView->GenereateFromAtom($result);
				}
			}
			
			$xhtmlBuilder = new XhtmlBuilder ($xhtml, $fzXmlView->GetStyle(), $title, $errorMessage);
			return $xhtmlBuilder;
			
		}
	}